public class test {
	//给定一个升序排列的整数数组nums和一个目标值target，请实现一个函数，返回target在数组中的索引
	public static int func(int[] arr,int target)
	{
		int left=0;
		int right=arr.length-1;
		int mid;
		while(left<=right)
		{
			//如果数组元素非常大为了避免溢出可以采取如下方法
			//mid=left+(right-left)/2;
			mid=(left+right)/2;
			if(arr[mid]>target)
			{
				right=mid-1;
			} else if (arr[mid]<target) {
				left=mid+1;

			}
			else {
				return mid;
			}
		}
		return -1;
	}

	public static void main(String[] args) {
		int[] arr={1,3,5,7,9};
		int target1=3;
		int target2=7;
		int target3=4;
		System.out.println(func(arr,target1));
		System.out.println(func(arr,target2));
		System.out.println(func(arr,target3));

	}
}
